<!--服务中心-->
<template>
  <div class="service-center">
    <!-- 搜索栏 -->
    <el-row :gutter="20" class="search-bar">
      <el-col :span="16">
        <el-input 
          v-model="searchQuery" 
          placeholder="输入关键词快速查找答案"
          clearable
        >
          <template #append>
            <el-button icon="Search" @click="handleSearch"></el-button>
          </template>
        </el-input>
      </el-col>
      <el-col :span="8" class="text-right">
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-popover placement="bottom" trigger="click">
          <template #reference>
            <el-button>高级筛选</el-button>
          </template>
          <el-form label-width="120px">
            <el-form-item label="服务类型">
              <el-select v-model="typeFilter">
                <el-option label="全部" value=""></el-option>
                <el-option label="技术咨询" value="tech"></el-option>
                <el-option label="售后支持" value="after-sale"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="紧急程度">
              <el-radio-group v-model="priorityFilter">
                <el-radio label="">全部</el-radio>
                <el-radio label="high">高优先级</el-radio>
                <el-radio label="normal">普通</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </el-popover>
      </el-col>
    </el-row>

    <!-- 动态提示 -->
    <transition name="fade">
      <div 
        v-if="!filteredTickets.length && searchQuery"
        class="no-results"
      >暂无匹配结果，请尝试其他关键词</div>
    </transition>

    <!-- 服务分类 -->
    <el-tabs v-model="activeTab" class="service-tabs">
      <el-tab-pane label="工单系统" name="tickets">
        <el-table :data="displayedTickets" style="width: 100%" stripe>
          <el-table-column prop="title" label="标题" width="300"></el-table-column>
          <el-table-column prop="type" label="类型" width="120">
            <template #default="{ row }">
              <el-tag :type="row.type === 'tech' ? 'info' : 'warning'">{{ row.type }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" width="120">
            <template #default="{ row }">
              <el-tag :type="row.status === 'pending' ? 'warning' : (row.status === 'resolved' ? 'success' : 'danger')">{{ row.status }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createdAt" label="创建时间" width="180"></el-table-column>
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button size="small" @click="viewTicket(row)">查看详情</el-button>
              <el-button size="small" type="primary" @click="replyTicket(row)" v-if="row.status === 'pending'">回复</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="知识库" name="knowledge">
        <el-collapse v-model="activeKnowledge">
          <el-collapse-item 
            v-for="(item, index) in filteredFAQs" 
            :key="index" 
            :title="item.title"
            :name="index"
          >
            <div v-html="item.content"></div>
          </el-collapse-item>
        </el-collapse>
      </el-tab-pane>
      <el-tab-pane label="服务记录" name="history">
        <el-timeline>
          <el-timeline-item 
            v-for="(activity, index) in serviceHistory" 
            :key="index" 
            :timestamp="activity.time"
          >
            {{ activity.description }}
          </el-timeline-item>
        </el-timeline>
      </el-tab-pane>
    </el-tabs>

    <!-- 工单详情弹窗 -->
    <el-dialog 
      v-model="ticketDialogVisible" 
      title="工单详情" 
      width="60%"
    >
      <el-descriptions column="1">
        <el-descriptions-item label="标题">{{ currentTicket.title }}</el-descriptions-item>
        <el-descriptions-item label="内容"><div v-html="currentTicket.content"></div></el-descriptions-item>
        <el-descriptions-item label="状态"><el-tag :type="currentTicket.status === 'pending' ? 'warning' : 'success'">{{ currentTicket.status }}</el-tag></el-descriptions-item>
      </el-descriptions>
      <el-divider content-position="left">回复记录</el-divider>
      <el-timeline>
        <el-timeline-item 
          v-for="(reply, index) in currentReplies" 
          :key="index" 
          :timestamp="reply.createdAt"
        >
          <p>{{ reply.message }}</p>
        </el-timeline-item>
      </el-timeline>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="ticketDialogVisible = false">关闭</el-button>
          <el-button type="primary" @click="resolveTicket">解决工单</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 知识库搜索 -->
    <el-dialog 
      v-model="knowledgeSearchVisible" 
      title="知识库检索" 
      width="40%"
    >
      <el-input 
        v-model="faqSearch" 
        placeholder="输入关键词"
        clearable
      >
        <template #append>
          <el-button icon="Search" @click="filterFAQs"></el-button>
        </template>
      </el-input>
      <el-collapse v-model="activeKnowledge">
        <el-collapse-item 
          v-for="(item, index) in filteredFAQs" 
          :key="index" 
          :title="item.title"
          :name="index"
        >
          <div v-html="item.content"></div>
        </el-collapse-item>
      </el-collapse>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue';
import { ElMessage } from 'element-plus';

const searchQuery = ref('');
const typeFilter = ref('');
const priorityFilter = ref('');
const activeTab = ref('tickets');
const ticketDialogVisible = ref(false);
const knowledgeSearchVisible = ref(false);
const faqSearch = ref('');

// 示例数据
const tickets = [
  // 填充示例数据...
];

const FAQs = [
  // 填充示例数据...
];

const serviceHistory = [
  // 填充示例数据...
];

// 过滤器应用
const filteredTickets = computed(() => {
  let filtered = tickets;
  if (searchQuery.value) filtered = filtered.filter(t => t.title.includes(searchQuery.value));
  if (typeFilter.value) filtered = filtered.filter(t => t.type === typeFilter.value);
  if (priorityFilter.value) filtered = filtered.filter(t => t.priority === priorityFilter.value);
  return filtered;
});

const filteredFAQs = computed(() => {
  return FAQs.filter(faq => faq.title.toLowerCase().includes(faqSearch.value.toLowerCase()));
});

// 分页逻辑
const currentPage = ref(1);
const pageSize = 10;
const displayedTickets = computed(() => {
  return filteredTickets.value.slice(
    (currentPage.value - 1) * pageSize,
    currentPage.value * pageSize
  );
});

// 查看工单
const currentTicket = reactive({});
const currentReplies = ref([]);
const viewTicket = (row) => {
  Object.assign(currentTicket, row);
  currentReplies.value = row.replies || [];
  ticketDialogVisible.value = true;
};

// 回复工单
const replyTicket = (row) => {
  // 打开回复表单
};

// 解决工单
const resolveTicket = () => {
  // 更新工单状态
  currentTicket.status = 'resolved';
  ElMessage.success('工单已解决');
  ticketDialogVisible.value = false;
};

// 筛选知识库
const filterFAQs = () => {
  // 应用关键词过滤
};
</script>

<style scoped>
.service-center {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.search-bar {
  margin-bottom: 20px;
}

.text-right {
  text-align: right;
}

.no-results {
  text-align: center;
  color: #999;
  margin-top: 20px;
}

.service-tabs {
  margin-top: 20px;
}

.el-table {
  margin-bottom: 20px;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>